﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.IO;

namespace ScreenBroadcast
{
    public class ScreenCapture : MarshalByRefObject
    {
        [DllImport("user32.dll")]
        private static extern IntPtr GetDesktopWindow();

        [DllImport("gdi32.dll")]
        private static extern bool BitBlt(
            IntPtr hdcDest, // handle to destination DC
            int nXDest, // x-coord of destination upper-left corner
            int nYDest, // y-coord of destination upper-left corner
            int nWidth, // width of destination rectangle
            int nHeight, // height of destination rectangle
            IntPtr hdcSrc, // handle to source DC
            int nXSrc, // x-coordinate of source upper-left corner
            int nYSrc, // y-coordinate of source upper-left corner
            Int32 dwRop // raster operation code
            );
        private const Int32 SRCCOPY = 0xCC0020;
        [DllImport("user32.dll")]
        private static extern int GetSystemMetrics(int nIndex);

        private const int SM_CXSCREEN = 0;
        private const int SM_CYSCREEN = 1;

        public Size GetDesktopBitmapSize()
        {
            return new Size(GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
        }
        public byte[] GetDesktopBitmapBytes()
        {
            var DesktopBitmapSize = GetDesktopBitmapSize();
            var Graphic = Graphics.FromHwnd(GetDesktopWindow());
            var MemImage = new Bitmap(DesktopBitmapSize.Width, DesktopBitmapSize.Height, Graphic);

            var MemGraphic = Graphics.FromImage(MemImage);
            var dc1 = Graphic.GetHdc();
            var dc2 = MemGraphic.GetHdc();
            BitBlt(dc2, 0, 0, DesktopBitmapSize.Width, DesktopBitmapSize.Height, dc1, 0, 0, SRCCOPY);
            Graphic.ReleaseHdc(dc1);
            MemGraphic.ReleaseHdc(dc2);
            Graphic.Dispose();
            MemGraphic.Dispose();

            var g = Graphics.FromImage(MemImage);
            var cur = System.Windows.Forms.Cursors.Arrow;
            cur.Draw(g, new Rectangle(System.Windows.Forms.Cursor.Position.X - 10, System.Windows.Forms.Cursor.Position.Y - 10, cur.Size.Width, cur.Size.Height));

            var ms = new MemoryStream();
            MemImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            return ms.GetBuffer ();
        }


        public Image Get_Resized_Image(int w, int h,byte[] image)
        {
            var ms = new MemoryStream(image);
            
            var bt = Image.FromStream(ms); 
            try
            {
                var sizing = new Size(w, h);
                bt = new Bitmap(bt, sizing);

            }
            catch (Exception) { }
            return bt;

        }

        public float Difference(Image orginalImage, Image secoundImage)
        {
            try
            {
                float counter = 0;
                var bt1 = new Bitmap(orginalImage);
                var bt2 = new Bitmap(secoundImage);
                var sizeH = bt1.Size.Height;
                var sizeW = bt1.Size.Width;
                float total = sizeH*sizeW;
                for (var x = 0; x != sizeW; x++)
                {
                    for (var y = 0; y != sizeH; y++)
                    {
                        var pixelImage1 = bt1.GetPixel(x, y);
                        var pixelImage2 = bt2.GetPixel(x, y);
                        if (pixelImage1 != pixelImage2)
                            counter++;
                    }
                }
                return (counter/total)*100;
            }
            catch (Exception) { }
            return 0;
        }

    }
}
